<script setup>
import { useLayout } from '@/layout/composables/layout'
import { ref, computed } from 'vue'
import AppConfig from '@/layout/AppConfig.vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import {md5} from "js-md5";

const account = ref()
const password1 = ref('')
const password2 = ref('')

const router = useRouter()
const { layoutConfig } = useLayout()

let yanzheng = ref(false)

let accountIsOk = ref(false)

const user = async () => {
  try {
    const response = await axios.post(
      'http://localhost:8080/user/register',
      {
        account: account.value,
        password: md5(password1.value),

      },
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    )
    if (response.status === 200) {
      console.log('注册请求成功')
      router.push('/auth/login')
    } else {
      console.log('注册请求失败')
    }
  } catch (error) {
    console.error(error)
  }
}

const accept = () => {
  password1.value = ''
  password2.value = ''
  yanzheng.value = false
}

const acceptAccount = () => {
  accountIsOk.value = false
}

const register = () => {
  console.log(account)
  if (account.value == undefined || accept.value === "") {
    accountIsOk.value = true
  }else if (password1.value !== password2.value) {
    yanzheng.value = true
  }else if(password1.value === undefined || password1.value === ""){
    yanzheng.value = true
  } else {
    user()
  }
}

const logoUrl = computed(() => {
  return `/layout/images/${layoutConfig.darkTheme.value ? 'logo-white' : 'logo-dark'}.png`
})
const returnLogin = () => {
  router.push('/auth/login')
}
</script>

<template>
  <div class="container" style="display: flex; position: absolute; flex-direction: row">
    <img :src="logoUrl" alt="Sakai logo" class="icon" />
    <strong style="height: 100%" class="flex">城の林荫</strong>
  </div>
  <div
    class="surface-ground flex align-items-center justify-content-center min-h-screen min-w-screen overflow-hidden"
  >
    <div class="flex flex-column align-items-center justify-content-center">
      <div
        style="
          border-radius: 56px;
          padding: 0.3rem;
          background: linear-gradient(180deg, var(--primary-color) 10%, rgba(33, 150, 243, 0) 30%);
        "
      >
        <div class="w-full surface-card py-8 px-5 sm:px-8" style="border-radius: 53px">
          <div>
            <label for="email1" class="block text-900 text-xl font-medium mb-2">账号</label>
            <InputText
              id="email1"
              type="text"
              placeholder="请输入账号"
              class="w-full md:w-30rem mb-5"
              style="padding: 1rem"
              v-model="account"
            />
            <label for="password1" class="block text-900 font-medium text-xl mb-2">密码</label>
            <div>
              <Password
                id="password1"
                v-model="password1"
                placeholder="请输入密码"
                :toggleMask="true"
                class="w-full mb-3"
                inputClass="w-full"
                :inputStyle="{ padding: '1rem' }"
              ></Password>
            </div>
            <div>
              <Password
                id="password2"
                v-model="password2"
                placeholder="请再次输入密码"
                :toggleMask="true"
                class="w-full mb-3"
                inputClass="w-full"
                :inputStyle="{ padding: '1rem' }"
              ></Password>
            </div>
            <div class="flex-container" style="width: 100%; margin-top: 30px">
              <div class="item1">
                <Button
                  @click="register"
                  label="确认注册"
                  style="width: 100%"
                  class="p-3 text-xl"
                ></Button>
              </div>

              <div class="item2">
                <Button
                  ref=""
                  label="返回"
                  style="width: 100%; border: 1px; background-color: rgb(155, 151, 151)"
                  class="p-3 text-xl"
                  @click="returnLogin()"
                ></Button>
                <Dialog
                  v-model:visible="accountIsOk"
                  :style="{ width: '450px' }"
                  header="账号不能为空"
                  :modal="true"
                >
                  <template #footer>
                    <Button label="重新输入" icon="pi pi-check" text @click="accountIsOk = false" />
                  </template>
                </Dialog>
                <Dialog
                  v-model:visible="yanzheng"
                  :style="{ width: '450px' }"
                  header="两次输入密码不一致请重新输入"
                  :modal="true"
                >
                  <template #footer>
                    <Button label="重新输入" icon="pi pi-check" text @click="accept" />
                  </template>
                </Dialog>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <AppConfig simple />
</template>

<style scoped>
.flex-container {
  display: -webkit-flex;
  display: flex;
}

.item1 {
  -webkit-flex: 1;
  flex: 1;
  margin-right: 5px;
}

.item2 {
  -webkit-flex: 1;
  flex: 1;
  margin-left: 5px;
}


strong {
  font-size: 40px;
  margin: auto auto;
}
</style>
